package com.njupt.DynamicProgramming;

/**
 * @Author: wujiaming
 * @CreateTime: 2025/2/23 20:53
 * @Description: 188. 买卖股票的最佳时机 IV
 * @Version: 1.0
 */


public class MaxProfit4_188 {

    public int maxProfit(int k, int[] prices) {
        int len = prices.length;
        int[][] dp = new int[len][2 * k + 1];
        dp[0][0] = 0;
        for (int j = 1; j < 2 * k + 1; j += 2) {
            dp[0][j] = 0 - prices[0];
        }

        for (int i = 1; i < len; i++) {
            for (int j = 1; j < 2 * k +1; j++) {
                if(j % 2 != 0){
                    dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-1] - prices[i]);
                }else {
                    dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-1] + prices[i]);
                }

            }
        }
        return dp[len-1][2 * k];
    }

    public static void main(String[] args) {
        int[] prices = {3,2,6,5,0,3};
        MaxProfit4_188 test = new MaxProfit4_188();
        System.out.println(test.maxProfit(2, prices));
    }
}
